<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="专业编号" prop="code">
      <el-input v-model="dataForm.code" placeholder="专业编号"></el-input>
    </el-form-item>
    <el-form-item label="专业名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="专业名称"></el-input>
    </el-form-item>
    <el-form-item label="主任名字" prop="directorName">
      <el-input v-model="dataForm.directorName" placeholder="主任名字"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="专业联系方式"></el-input>
    </el-form-item>
    <el-form-item label="人数" prop="count">
      <el-input type="number" min="0" v-model="dataForm.count" placeholder="人数"></el-input>
    </el-form-item>
    <el-form-item label="院系ID" prop="depId">
      <el-input v-model="dataForm.depId" placeholder="院系ID"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          directorName: '',
          phone: '',
          count: '',
          createTime: '',
          updateTime: '',
          depId: '',
          isDelete: ''
        },
        dataRule: {
          code: [
            { required: true, message: '专业编号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '专业名称不能为空', trigger: 'blur' }
          ],
          directorName: [
            { required: true, message: '主任名字不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '专业联系方式不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '人数不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          depId: [
            { required: true, message: '院系ID不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/stu/major/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.major.code
                this.dataForm.name = data.major.name
                this.dataForm.directorName = data.major.directorName
                this.dataForm.phone = data.major.phone
                this.dataForm.count = data.major.count
                this.dataForm.createTime = data.major.createTime
                this.dataForm.updateTime = data.major.updateTime
                this.dataForm.depId = data.major.depId
                this.dataForm.isDelete = data.major.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/stu/major/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'directorName': this.dataForm.directorName,
                'phone': this.dataForm.phone,
                'count': this.dataForm.count,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'depId': this.dataForm.depId,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
